/*
采用 getaddrinfo 传入主机名和服务名实现的 UDP 时间获取客户端。
**/

#include <define.h>
#include <error.h>
#include <wrapfunc.h>
#include <sockfunc.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stddef.h>
#include <time.h>


int
main(int argc, char **argv) {
    if (argc != 3)
        error_quit("usage: %s host service", argv[0]);
    const char *host = argv[1];
    const char *service = argv[2];

    int sockfd = Udp_connect(host, service);
    struct sockaddr_storage ss;
    socklen_t len = sizeof(ss);
    Getpeername(sockfd, (struct sockaddr*)&ss, &len);
    error_msg("udp connect to %s", sock_ntop((struct sockaddr*)&ss, len));

    Write(sockfd, "", 1);
    char buf[MAX_LINE];
    size_t n = Read(sockfd, buf, sizeof(buf));
    buf[n] = 0;
    error_msg("server time:%s", buf);
    Close(sockfd);
    
    return 0;
}
